﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using CMP.ERP.Model;
using CMP.ERP.BLL;

namespace CMP.ERP.Admin.Controllers
{
    public class ProviderController : Controller
    {
        //
        // GET: /Provider/
        /// <summary>
        /// 实例化ExpressCompanyBLL对象
        /// </summary>
        ExpressCompanyBLL bll = new ExpressCompanyBLL();

        /// <summary>
        /// 快递公司信息显示
        /// </summary>
        /// <returns></returns>
        public ActionResult Provider_show()
        {
            List<ExpressCompany> list = new List<ExpressCompany>();
            DataTable data = bll.ExpressCompanyShow();
            foreach(DataRow item in data.Rows)
            {
                ExpressCompany e = new ExpressCompany();
                e.Express_Company_Id = Convert.ToInt32(item[0]);
                e.Express_Company_Name = item[1].ToString();
                e.Express_Company_Charges = item[2].ToString();
                e.Express_Company_Address = item[3].ToString();
                e.Express_Company_Phone = item[4].ToString();
                list.Add(e);
            }
            return View(list);
        }
        /// <summary>
        /// 根据名称查询快递公司信息
        /// </summary>
        /// <param name="Express_Company_Name"></param>
        /// <returns></returns>
        public ActionResult SelExpressCompany(string Express_Company_Name)
        {
            List<ExpressCompany> list = new List<ExpressCompany>();
            DataTable data = bll.SelExpressCompany(Express_Company_Name);
            foreach (DataRow item in data.Rows)
            {
                ExpressCompany e = new ExpressCompany();
                e.Express_Company_Id = Convert.ToInt32(item["Express_Company_Id"]);
                e.Express_Company_Name = item["Express_Company_Name"].ToString();
                e.Express_Company_Charges = item["Express_Company_Charges"].ToString();
                e.Express_Company_Address = item["Express_Company_Address"].ToString();
                e.Express_Company_Phone = item["Express_Company_Phone"].ToString();
                list.Add(e);
            }
            return View("Provider_show", list);
        }
        /// <summary>
        /// 弹出添加快递公司信息视图
        /// </summary>
        /// <returns></returns>
        public ActionResult AddExpressCompany()
        {
            return View();
        }
        /// <summary>
        /// 实现添加快递公司信息功能
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddExpressCompany(ExpressCompany s)
        {
            if (bll.AddExpressCompany(s) > 0)
            {
                return Content("<script>alert('添加成功!');location.href='/Provider/Provider_show'</script>");
            }
            else
            {
                return Content("<script>alert('添加失败!');location.href='/Provider/AddExpressCompany'</script>");
            }
        }
        /// <summary>
        /// 删除快递公司信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult DelExpressCompany(int id)
        {
            if (bll.DelExpressCompany(id)>0)
            {
                return Content("<script>alert('删除成功!');location.href='/Provider/Provider_show'</script>");
            }
            else
            {
                return Content("<script>alert('删除失败!');location.href='/Provider/Provider_show'</script>");
            }
        }
        /// <summary>
        /// 弹出修改快递公司信息视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult UptExpressCompany(int id)
        {
            ExpressCompany e = new ExpressCompany();
            DataTable data = bll.GetExpressCompany(id);
            foreach (DataRow item in data.Rows)
            {
                e.Express_Company_Id = Convert.ToInt32(item["Express_Company_Id"]);
                e.Express_Company_Name = item["Express_Company_Name"].ToString();
                e.Express_Company_Charges = item["Express_Company_Charges"].ToString();
                e.Express_Company_Address = item["Express_Company_Address"].ToString();
                e.Express_Company_Phone = item["Express_Company_Phone"].ToString();
            }
            return View(e);
        }
        /// <summary>
        /// 实现修改快递公司信息功能
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UptExpressCompany(ExpressCompany s)
        {
            if (bll.UpdExpressCompany(s) > 0)
            {
                return Content("<script>alert('修改成功!');location.href='/Provider/Provider_show'</script>");
            }
            else
            {
                return Content("<script>alert('修改失败!');location.href='/Provider/UptExpressCompany'</script>");
            }
        }
    }
}
